package com.fz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fz.core.FzException;
import com.fz.entity.ParkingSpace;
import com.fz.entity.Repair;
import com.fz.mapper.ParkingSpaceMapper;
import com.fz.service.ParkingSpaceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fz
 * @since 2024-08-26
 */
@Service
public class ParkingSpaceServiceImpl extends ServiceImpl<ParkingSpaceMapper, ParkingSpace> implements ParkingSpaceService {
    @Resource
    ParkingSpaceMapper parkingSpaceMapper;
    @Override
    public Boolean reject(Long id, Integer status){
        return this.updateById(new ParkingSpace(id,status));
    }
    @Override
    public Boolean pass(Long id, Integer status){
        return this.updateById(new ParkingSpace(id,status));
    }


    @Override
    public Boolean check(String field, String value) {
        QueryWrapper<ParkingSpace> wrapper = new QueryWrapper<>();
        wrapper.eq(field,value);

        return this.count(wrapper)==0;
    }
    @Override
    public Boolean del(Long id)
    {
        if (id == null) {
            throw new FzException("车位不存在");
        }
        ParkingSpace parkingSpace = this.getById(id);
        parkingSpace.setCarNumber("");
        parkingSpace.setUsername("");
        parkingSpace.setOccupied(0);
        return this.updateById(parkingSpace);
    }

    @Override
    public Boolean update(Long id, String carNumber, String username, Integer occupied)
    {
        if(park(username)>=3){
            throw new FzException("车位申请不可超过三个");
        }
        ParkingSpace parkingSpace = this.getById(id);
        if (id == null) {
            throw new FzException("车位不存在");
        }
        if(parkingSpace.getOccupied()==1||parkingSpace.getOccupied()==2)
        {
            throw new FzException("车位已被占用");
        }
        parkingSpace.setCarNumber(carNumber);
        parkingSpace.setUsername(username);
        parkingSpace.setOccupied(occupied);
        return this.updateById(parkingSpace);

    }
    @Override
    public Page<ParkingSpace> search(String input, Integer pageNo, Integer pageSize){
        QueryWrapper<ParkingSpace> wrapper=new QueryWrapper<>();
        Page<ParkingSpace>parkingSpacePage;
        wrapper.like("username",input)
                .ne("occupied",0);
        parkingSpacePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Page<ParkingSpace> showList(Integer pageNo, Integer pageSize) {
        QueryWrapper<ParkingSpace> wrapper = new QueryWrapper<>();
        Page<ParkingSpace>parkingSpacePage;
        wrapper.ne("occupied",0);
        parkingSpacePage = this.page(new Page<>(pageNo, pageSize), wrapper);
        return this.page(new Page<>(pageNo, pageSize),wrapper);
    }
    @Override
    public Integer park(String input){
        QueryWrapper<ParkingSpace> wrapper = new QueryWrapper<>();
        wrapper.like("username", input);
        return Math.toIntExact(this.count(wrapper));

    }



}
